package org.jeecg.modules.inspect.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.inspect.service.IInsTaskItemService;
import org.jeecg.modules.inspect.service.IInsTaskService;
import org.jeecg.modules.inspect.vo.DepartReportVo;
import org.jeecg.modules.inspect.vo.MonthReportVo;
import org.jeecg.modules.inspect.vo.RoleReportVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @Description: 任务
* @Author: jeecg-boot
* @Date:   2023-11-24
* @Version: V1.0
*/
@Api(tags="任务")
@RestController
@RequestMapping("/inspect/report")
@Slf4j
public class ReportController {

   @Autowired
   private IInsTaskService insTaskService;

   @Autowired
   private IInsTaskItemService insTaskItemService;

    @Resource
    private ISysBaseAPI sysBaseApi;

    @ApiOperation(value="厂领导报表", notes="厂领导报表")
    @GetMapping(value = "/role")
    public Result<IPage<RoleReportVo>> role(RoleReportVo roleReportVo,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<RoleReportVo> page = new Page<RoleReportVo>(pageNo, pageSize);
        return Result.OK(insTaskService.roleReport(page, roleReportVo));
    }

    @ApiOperation(value="部门报表", notes="部门报表")
    @GetMapping(value = "/depart")
    public Result<IPage<DepartReportVo>> depart(DepartReportVo departReportVo,
                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<DepartReportVo> page = new Page<DepartReportVo>(pageNo, pageSize);

        return Result.OK(insTaskService.departReport(page, departReportVo));
    }

    @ApiOperation(value="月度报表", notes="月度报表")
    @GetMapping(value = "/month")
    public Result<IPage<MonthReportVo>> month(MonthReportVo monthReportVo,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<MonthReportVo> page = new Page<MonthReportVo>(pageNo, pageSize);

        return Result.OK(insTaskService.monthReport(page, monthReportVo));
    }

    /**
     * 导出厂领导任务报表
     * @return
     */
    @RequestMapping(value = "/exportRoleXls")
    public ModelAndView exportRoleXls(HttpServletRequest request, RoleReportVo roleReportVo) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.1 组装查询条件
        Page<RoleReportVo> page = new Page<RoleReportVo>(1, 99999);
        IPage<RoleReportVo> roleReportVoIPage = insTaskService.roleReport(page, roleReportVo);
        List<RoleReportVo> records = roleReportVoIPage.getRecords();

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "厂领导任务报表");
        mv.addObject(NormalExcelConstants.CLASS, RoleReportVo.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("厂领导任务报表", "导出人:" + sysUser.getRealname(), "厂领导任务报表"));
        mv.addObject(NormalExcelConstants.DATA_LIST, records);
        return mv;
    }

    /**
     * 导出部门任务报表
     * @return
     */
    @RequestMapping(value = "/exportDepartXls")
    public ModelAndView exportDepartXls(HttpServletRequest request, DepartReportVo departReportVo) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.1 组装查询条件
        Page<DepartReportVo> page = new Page<DepartReportVo>(1, 99999);
        IPage<DepartReportVo> departReportVoIPage = insTaskService.departReport(page, departReportVo);
        List<DepartReportVo> records = departReportVoIPage.getRecords();

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "部门任务报表");
        mv.addObject(NormalExcelConstants.CLASS, DepartReportVo.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("部门任务报表", "导出人:" + sysUser.getRealname(), "部门任务报表"));
        mv.addObject(NormalExcelConstants.DATA_LIST, records);
        return mv;
    }

    /**
     * 导出月度任务报表
     * @return
     */
    @RequestMapping(value = "/exportMonthXls")
    public ModelAndView exportMonthXls(HttpServletRequest request, MonthReportVo monthReportVo) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.1 组装查询条件
        Page<MonthReportVo> page = new Page<MonthReportVo>(1, 99999);
        IPage<MonthReportVo> monthReportVoIPage = insTaskService.monthReport(page, monthReportVo);
        List<MonthReportVo> records = monthReportVoIPage.getRecords();

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "月度任务报表");
        mv.addObject(NormalExcelConstants.CLASS, MonthReportVo.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("月度任务报表", "导出人:" + sysUser.getRealname(), "月度任务报表"));
        mv.addObject(NormalExcelConstants.DATA_LIST, records);
        return mv;
    }

}
